//
//  UserMyViewController.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "UserMyViewController.h"
#import "LoginViewController.h"
#import "MovieCollectViewController.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "DataBaseSingleton.h"

@interface UserMyViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain)MBProgressHUD *hud;

@end

@implementation UserMyViewController

- (void)dealloc
{
    [_myTableView release];
    [_hud release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的";
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 60) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
#pragma mark -- 注册
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
#pragma mark -- 右按钮
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
#warning - 不知道的小秘密:去掉多余的cell
    self.myTableView.tableFooterView = [UIView new];
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *isLogin = [NSUSER objectForKey:@"isLogin"];
    if ([@"0" isEqualToString:isLogin]) {
        self.navigationItem.rightBarButtonItem.title = @"登陆";
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    } else {
        self.navigationItem.rightBarButtonItem.title = @"注销";
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    }
}

#pragma mark -- 右按钮
- (void)rightAction:(UIBarButtonItem *)button{
    NSString *isLogin = [NSUSER objectForKey:@"isLogin"];
// 判断登陆状态
    if ([@"0" isEqualToString:isLogin]) {
    // 如果未登录
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
        [naVC release];
    } else {
        // 已登陆
        UIAlertController *alertV = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
            [NSUSER setValue:@"0" forKey:@"isLogin"];
            self.navigationItem.rightBarButtonItem.title = @"登陆";
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            return;
            
        }];
        [alertV addAction:action1];
        [alertV addAction:action2];
        [self presentViewController:alertV animated:YES completion:^{
            
        }];
    }
}
#pragma mark -- 区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark -- 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
#pragma mark -- cell设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdntifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdntifier];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的电影";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"清除缓存";
    }
    
    return cell;
}
#pragma mark -- 点击cell触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *isLogin = [NSUSER objectForKey:@"isLogin"];
#pragma mark -- 点击电影
    MovieCollectViewController *movieCVC = [[MovieCollectViewController alloc] init];
    if (indexPath.row == 0) {
        // 判断登陆状态
        if (![@"1" isEqualToString:isLogin]) {
            // 如果未登录
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:naVC animated:YES completion:^{
                
            }];
            [naVC release];
        } else {
            [self.navigationController pushViewController:movieCVC animated:YES];
        }
        [movieCVC release];
    }
#pragma mark -- 点击清除缓存
    // 获取SDWebImage缓存图片大小
    NSUInteger size = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = size / 1024.0 / 1024.0;
    NSString *string = [NSString stringWithFormat:@"缓存大小为%.2fMB", cacheSize];
    if (indexPath.row == 2) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleActionSheet];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"清除缓存" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            // 清除缓存
            [[SDImageCache sharedImageCache] clearDisk];
            // hud单例
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"清除缓存成功";
            self.hud.mode = MBProgressHUDModeText;
            [self.hud hide:YES afterDelay:1];
            
        }];
        // 创建提示框按钮
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
        [alertC addAction:action1];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
